class PolyNode:
    def __init__(self, x=0, y=0, next=None):
        self.coefficient = x
        self.power = y
        self.next = next


class Solution:
    def addPoly(self, poly1, poly2):
        prev_head = PolyNode(0, 0, poly1)
        prev = prev_head
        while poly1 or poly2:
            if poly1:
                if poly2:
                    if poly1.power == poly2.power:
                        poly1.coefficient += poly2.coefficient
                        if poly1.coefficient == 0:
                            prev.next = poly1.next
                        else:
                            prev.next = poly1
                            prev = poly1
                        poly1 = poly1.next
                        poly2 = poly2.next
                    else:
                        if poly1.power > poly2.power:
                            prev.next = poly1
                            prev = poly1
                            poly1 = poly1.next
                        else:
                            prev.next = poly2
                            prev = poly2
                            poly2 = poly2.next
                else:
                    prev.next = poly1
                    prev = poly1
                    poly1 = poly1.next
            else:
                prev.next = poly2
                prev = poly2
                poly2 = poly2.next
        return prev_head.next
